/* $Header: p_struct.c,v 2.2 01/10/02 10:27:53 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:                                                        */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE:                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/
#include <typedefs.h>
#include <wkfile.h>
#include <pcb_defs.h>

#include <typedefs.h>
#include <std_ops.h>
#include <status.h>
#include <error.h>
#include <beep.h>

#include <display_ui.h>
#include <display.h>

#include <exception.h>

#include <task_pub.h>
#include <tasks.h>

#include  <p_config.h>
#include  <p_status.h>
#include  <p_ui.h>
#include  <m68332.h>
#include "test/m_penuLoad.h"
#include "test/hz512.h"
#include "test/socket.h"
#include <math.h>

int main(int argc, char *argv[])
{
/*--------Init drivers/interrupts/tasks--------------*/
//  float a = exp(1.0);
//  U_INT8 test[10] = {1,2,3,4,5,6,7,8,9,10};
//  printf("test[2]:%d  a= %f\n",tableU(test,3),a);
  int ret = 0;
  char* a = (char*)(&ret);
  *a = 0;
  *(a+1) = 1;
  printf("ret = %d\n",ret);
  ret = Startup();
  if(ret < 0)
  {
    mLog("Startup failed!!!\n");
    return ret;
  }
/*-------------Main Run------------------*/

  printf("main TASK:%x  !!\n",pthread_self());
  while(1)
  {
/*
    char string [256];
    char* p;
    int i=0,j=0;
    pQUEUE_ENTRY buffer;
    U_INT16 result16 = 10;
    BIT8 result8 = 0; 
    U_INT32 result32 = 10;
    mLog("----free_Queue:%d  Cmd_Queue:%d  Resp_Queue:%d-----\n", Pneu_Free_Pool.Num_Entries,Pneu_Cmd_Queue.Num_Entries,Pneu_Resp_Queue.Num_Entries);
    mLog("please write the 0-15 to send cmds, 16 for help:\n");
    gets (string);
    int a = atoi(string);    
*/
    
    Task_yield ();   /* Yield cpu to rest of background */
/*
    switch(a)
    {
    case 1:
      DownloadSetpoints();
      break;
    case 2:
      DownloadPids(FRNT_INLET_PRESSURE_CHANNEL,1,1,1,1);
      mLog("DownloadPids\n");
      break;
    case 3:
      PcbPerformSelftest( &result16 );
      mLog("PcbPerformSelftest :%d\n",result16);
      break;
    case 4:
      mLog("input the read Module Number:");
      gets (string);
      i = atoi(string);
      mLog("input the address:");
      gets (string);
      j = atoi(string);
      ReadEeprom( i, j, &result16 );
      mLog("ReadEeprom :%04x\n",result16);
      break;
    case 5:
      mLog("input the wirte Module Number:");
      gets (string);
      i = atoi(string);
      InitDetEEPROM(i,EPC_FID,0xa854,0x00a0,0x0000,0x0001,0xc282);
      mLog("write Eeprom to defaut Det!\n");
      break;
    case 6:
      WriteDutyCycle(FRNT_INLET_PRESSURE_CHANNEL,0);
      mLog("WriteDutyCycle FRNT_INLET_PRESSURE_CHANNEL 0!\n");
      break;
    case 7:
      result8 = ReadDutyCycles();
      mLog("ReadDutyCycles answer:%d!\n",result8);
      break;
    case 8:
      RestartAllChannels();
      mLog("RestartAllChannels!\n");
      break;
    case 9:
      PcbPeek( INLET_P_SENSOR,&result32 );
      mLog("PcbPeek!\n");
      break;
    case 10:
      PcbPoke( INLET_P_SENSOR,0);
      mLog("PcbPoke!\n");
      break;
    case 11:
      RequestEnvironment();
      mLog("RequestEnvironment!\n");
      break;
    case 12:
      RequestICapCounters();
      mLog("RequestICapCounters!\n");
      break;
    case 13:
      GetBuffer(buffer, &Pneu_Free_Pool);
      mLog("please enter the command,such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15(the bit 16 is checksum  calced automatically)!\n");
      gets (string);
      p = strtok( string, " " );
      mLog( "change to hex bytes:");
      while( p != NULL )
      {
        buffer->PCB_Command[i] = atoi(p);
        mLog( " %04x", buffer->PCB_Command[i]);
        i++;
        if(i>14)
          break;
        p = strtok( NULL, " " );
      }
      for(j = i; j<15;j++)
        mLog( " %02x", buffer->PCB_Command[j]);

      buffer->PCB_Command[15] = 0;
      for(i=0;i<15;i++)
        buffer->PCB_Command[15] += buffer->PCB_Command[i];
      mLog( " %04x\n", buffer->PCB_Command[15]);
      buffer->Return_Queue = NULL;
      LinkBufferToTail(buffer, &Pneu_Cmd_Queue);

      break;
    case 0:
      mLog("\
1   Download Setpoint     13 channel setpoints and fwd/bkwd mask, report actuals \n\
2   Download PID          pneumatic channel #, four PID terms\n\
3   Self Test             perform self test of A/D\n\
4   Read EEPROM           EEPROM address and Pneumatic Module\n\
5   Write EEPROM          EEPROM address,data and Pneumatic Module\n\
6   Set Duty Cycle        pneumatic channel #, duty cycle value, set or clear\n\
7   Read Duty Cycle       report all pneumatic channel duty cycles\n\
8   Restart Channels      pneumatic channel #'s to restart after shutdown\n\
9   Peek                  DSP Processor location to read\n\
10  Poke                  DSP Processor location and value to write\n\
11  Request Environment   report background channels (+10v,offset,BPS,etc.)\n\
12  Request I-Cap Counter report integral term cap counters (non-zero  = not in control)\n\
13  write the cmd manually!!!!!\n");
      break;
    default:
      break;
    }
*/
  }
/*--------------------Deinit-----------------*/
  sleep(1);
  return ret;
}
